package com.baizhi.controller;

import com.baizhi.dto.response.StudentScoreDTO;
import com.baizhi.entity.Course;
import com.baizhi.entity.StudentCourse;
import com.baizhi.service.CourseService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
@Tag(name="课程管理模块",description = "课程管理的控制器")
public class ConrseController {

    @Autowired
    private CourseService courseService;




    @PostMapping("/update")
    public ResponseEntity<String> updateCourse(@RequestBody Course course){
        Integer row = courseService.update(course);
        if(row!=0){
            return ResponseEntity.ok("修改成功");
        }else{
            return ResponseEntity.notFound().build(); //404
        }
    }


    @GetMapping("/score")
    public List<StudentScoreDTO> queryScoreByUserIdAndCourseId(@RequestParam("userId") Integer userId) {
        List<StudentScoreDTO> studentScoreDTOS = courseService.queryScoreByUserIdAndCourseId(userId);
        return studentScoreDTOS;
    }

    @GetMapping("/deleteById")
    public ResponseEntity<String> deleteMyCourseByUserIdAndCourseId(@RequestParam("userId") Integer userId, @RequestParam("courseId") Integer courseId){

        Integer row = courseService.deleteMyCourseByUserIdAndCourseId(userId, courseId);
        if(row!=0){
            return new ResponseEntity<>("成功",HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity<>("失败",HttpStatusCode.valueOf(500));
        }
    }

    @GetMapping("/byUserId")
    public List<Course> getCoursesByUserId(@RequestParam("userId") Integer userId){
        return courseService.getCoursesByUserId(userId);
    }

    @GetMapping("/add")
    public ResponseEntity<String> addCourse(@Valid StudentCourse studentCourse){
        Integer row = courseService.addCourse(studentCourse.getUserId(),studentCourse.getCourseId());
        if(row !=0){
            return new ResponseEntity<>("成功", HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity<>("失败",HttpStatusCode.valueOf(500));
        }
    }

    @GetMapping
    public List<Course> getAllCourses(){
        List<Course> list = courseService.list();
        return list;
    }

}
